package com.zrfintech.manage.controller.mg;

import cn.hutool.crypto.digest.MD5;
import com.google.common.collect.Maps;
import com.zrfintech.domain.MgUser;
import com.zrfintech.manage.constant.ErrorCode;
import com.zrfintech.manage.util.HttpResponseUtil;
import com.zrfintech.service.MgUserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private MgUserRepo mgUserRepo;


    @RequestMapping("/")
    public String login() {
        return "/login.html";
    }

    @ResponseBody
    @RequestMapping(value = "/simpleLogin", method = RequestMethod.POST)
    public Object simpleLogin(HttpServletResponse response,
                              @RequestBody MgUser mgUser) {
        mgUser.setUserPassword(MD5.create().digestHex(mgUser.getUserPassword()));

        MgUser loginUser = mgUserRepo.getByUserCodeAndPassword(mgUser.getUserCode(), mgUser.getUserPassword());
        if (loginUser != null) {
            Map data = Maps.newConcurrentMap();
            data.put("user_code", loginUser.getUserCode());
            return data;
        } else {
            return HttpResponseUtil.error(ErrorCode.NO_LOGIN, "用户名或密码错误");
        }

    }
}
